package com.qpl.depression.cms.controller;

import com.qpl.depression.cms.common.ServerResponse;
import com.qpl.depression.cms.entity.CategoryLevel;
import com.qpl.depression.cms.service.CategoryLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class CategoryLevelController {
    @Autowired
    private CategoryLevelService categoryLevelService;
    private Long id;

    /**
     * 跳转到分数级别管理
     *
     * @return 管理页面
     */
    @GetMapping("level/page")
    public String levelPage(Long categoryId, ModelMap map) {
        map.addAttribute("categoryId", categoryId);
        id = categoryId;
        return "categoryLevelListPage";
    }

    /**
     * 查询分数级别数据
     *
     * @param name 分类名
     * @return 查询结果
     */
    @GetMapping("level/data")
    @ResponseBody
    public ServerResponse levelData(String name) {
        List<CategoryLevel> levels = categoryLevelService.getByName(name, id);
        return ServerResponse.createBySuccess("success", levels);
    }

    /**
     * 跳转到新添加级别信息页面
     *
     * @return 页面
     */
    @GetMapping("level/category/add/page")
    public String addPage(ModelMap map) {
        map.addAttribute("categoryId", id);
        return "categoryLevelAddPage";
    }


    /**
     * 新增或者修改分类信息
     *
     * @param categoryLevel 分类信息
     * @return 修改结果
     */
    @PostMapping("level/category/add")
    @ResponseBody
    public ServerResponse<Object> levelCategoryAdd(CategoryLevel categoryLevel) {
        return categoryLevelService.saveOrUpdate(categoryLevel) ? ServerResponse.createBySuccessMessage("成功") : ServerResponse.createByErrorMessage("失败");
    }

    /**
     * 根据id删除分类详情
     *
     * @param id 分类id
     * @return 删除结果
     */
    @GetMapping("category/level/del")
    @ResponseBody
    public ServerResponse del(Long id) {
        return categoryLevelService.removeById(id) ? ServerResponse.createBySuccessMessage("成功") : ServerResponse.createByErrorMessage("失败");
    }

    /**
     * 试题等级编辑页面
     *
     * @return
     */
    @GetMapping("category/level/edit/page")
    public String editPage(Long id, ModelMap map) {
        CategoryLevel categoryLevel = categoryLevelService.getById(id);
        map.addAttribute("categoryLevel", categoryLevel);
        return "categoryLevelEditPage";
    }

}
